//
//  LoanFeeView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/12/17.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol LoanFeeViewDelegate {
    @objc optional func selectLoanMonth(cell:LoanFeeCell,monthCount:String) //选择贷款月份
    @objc optional func inputLoanMonth(footView:LoanFeeFootView) //输入贷款金额
}

class LoanFeeView: UIView {
    
    weak var delegate : LoanFeeViewDelegate?
    
    fileprivate var model = GenericLoansModel()
    
    fileprivate let cellId = "loanFreeCell"
    fileprivate var dataArray = [String]()
    fileprivate var tableView : UITableView!
    
    fileprivate var footView : LoanFeeFootView!
    
    init(model:GenericLoansModel) {
        super.init(frame: .zero)
        
        let minMonth = model.loan_min_time
        dataArray.append("\((Int(minMonth!) ?? 0) + 3)")
        dataArray.append("\((Int(minMonth!) ?? 0) + 6)")
        dataArray.append("\((Int(minMonth!) ?? 0) + 9)")
        
        self.model = model
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.layer.borderColor = UIColor.lightGray.cgColor
        tableView.layer.borderWidth = 1
        tableView.backgroundColor = UIColor.clear
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.isScrollEnabled = false
        tableView.register(LoanFeeCell.self, forCellReuseIdentifier: cellId)
        self.addSubview(tableView)
        
        let headView = LoanFeeHeadView.init(frame: .init(x: 0, y: 0, width: frame.size.width, height: RATIO_H(maxNum: 30)), model: model)
        tableView.tableHeaderView = headView
        
        footView = LoanFeeFootView.init(frame: .init(x: 0, y: 0, width: frame.size.width, height: RATIO_H(maxNum: 70)), model: model)
        footView.addTarget(self, action: #selector(customLoanMonth(sender:)), for: .touchUpInside)
        footView.delegate = self
        tableView.tableFooterView = footView
        
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
        
    }
    
    //MARK: 自定义贷款月份
    @objc fileprivate func customLoanMonth(sender:LoanFeeFootView) {
        
        sender.monthTextField.isUserInteractionEnabled = true
        sender.interestLabel.textColor = UIColor.loanFeeColor()
        tableView.reloadData()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension LoanFeeView : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return RATIO_H(maxNum: 70)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? LoanFeeCell
        cell?.monthLabel.text = "分" + dataArray[indexPath.row] + "个月"
        cell?.selectionStyle = .none
        cell?.updateStyle()
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        footView.monthTextField.isUserInteractionEnabled = false
        
        if footView.interestLabel.textColor == UIColor.loanFeeColor() {
            footView.interestLabel.textColor = UIColor.black
            footView.interestLabel.text = "总利息0.00元"
            footView.supplyLabel.text = "月供0.00元"
            footView.monthTextField.text = ""
        }
        
        let cell = tableView.cellForRow(at: indexPath) as? LoanFeeCell
        cell?.selectImageView.image = UIImage.init(named: "month_select")
        cell?.monthLabel.textColor = UIColor.loanFeeColor()
        cell?.interestLabel.textColor = UIColor.loanFeeColor()
        
        self.delegate?.selectLoanMonth!(cell: cell!, monthCount: dataArray[indexPath.row])
    }
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        
        let cell = tableView.cellForRow(at: indexPath) as? LoanFeeCell
        cell?.selectImageView.image = UIImage.init(named: "month_unselect")
        cell?.monthLabel.textColor = UIColor.black
        cell?.interestLabel.textColor = UIColor.black
        cell?.interestLabel.text = "总利息0.00元"
        cell?.supplyLabel.text = "月供0.00元"
    }
}

extension LoanFeeView : LoanFeeFootViewDelegate {
    
    //MARK: 自定义月份
    func customMonth() {
        
        footView.interestLabel.textColor = UIColor.loanFeeColor()
        tableView.reloadData()
    }
    
    //MARK: 输入贷款月份
    func inputLoanMonth() {
        
        self.delegate?.inputLoanMonth!(footView: footView)
    }
}
